<template>
  <DataList
    ref="datalist"
    :cate="cate"
    :cols="cols"
    :excelHeader="excelHeader"
    :name="name"
    skipExcelNumber="3"
  ><template slot="search">
      <el-form-item>
        <el-input
          v-model="search_str"
          placeholder="请输入教师名字搜索"
          clearable
          @change="clear"
          @input="mysearch"
        />
      </el-form-item>
    </template></DataList>
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {
  get教师基本情况ByGH,

  get教师基本情况
} from "@/api/教师基本情况";
import { get开设专业 } from "@/api/开设专业";
import { get课程设置 ,  get课程设置ByID} from "@/api/课程设置";
import {
  get教师授课情况,
  update教师授课情况,
  delete教师授课情况,
  add教师授课情况,
  bulkAddJsskqk
} from "@/api/教师授课情况";
import { findComponent } from "@/utils/ui";
import linq from "linq";
export default {
  components: {
    DataList
  },
  data() {
    return {
      search_str: "",
      cate: "A6教师授课情况表",
      name: "6.2 教师授课情况",
      excelHeader: {
        B3: "系部代码",
        C3: "所属系部",
        D3: "教工号",
        E3: "姓名",
        F3: "授课年级",
        G3: "专业代码",
        H3: "专业名称（全称）",
        I3: "专业方向代码",
        J3: "专业方向名称（全称）",
        K3: "课程代码",
        L3: "课程名称（全称）",
        M3: "课程类型",
        N3: "课程属性",
        O3: "授课任务",
        P3: "是否合班授课",
        Q3: "是否平行班授课",
        R3: "是否行政班授课",
        S3: "授课学期",
        T3: "教学工作量（学时）",
        U3: "主要开设实践项目名称(全称)",
        U4: "实验",
        V4: "实训",
        W4: "实习"
      },
      cols: [
        // { label: '教师所属系部代码', prop: 'BMBH' },

        { label: "教师所属系部名称", prop: "BMMC" },
        { label: "教师性质", prop: "教师性质" },
        { label: "教工号", prop: "GH" },
        { label: "姓名", prop: "XM" },
        { label: "授课年级", prop: "XZ_SKNJ" },
        { label: "专业代码", prop: "ZYDM" },
        { label: "专业名称(全称)", prop: "ZYMC" },
        { label: "专业方向代码", prop: "ZYFXDM" },
        { label: "专业方向名称(全称)", prop: "ZYFXMC" },
        { label: "课程代码", prop: "KCDM" },
        { label: "课程名称(全称)", prop: "KCMC" },
        { label: "课程类型", prop: "XZ_KCLX" },
        { label: "课程属性", prop: "XZ_KCSX" },
        { label: "授课任务", prop: "XZ_SKRW" },
        { label: "是否合班授课", prop: "XZ_SFHBSK" },
        { label: "是否平行班授课", prop: "XZ_SFPXB" },
        { label: "是否行政班授课", prop: "XZ_SFXZB" },
        { label: "授课学期", prop: "XZ_SKXQ" },
        { label: "教学工作量(学时)", prop: "JXGZL" },
        {
          label: "主要开设实践项目名称(全称)",
          prop: "主要开设实践项目名称",
          data: [
            { label: "实验", prop: "SHIYANXMMC" },
            { label: "实训", prop: "SHIXUNXMMC" },
            { label: "实习", prop: "SHIXIXMMC" }
          ]
        }
      ]
    };
  },
  methods: {
    getSummaries(list) {
      //
      const sums = [];
      this.cols.forEach((column, index) => {
        sums[index] = "";
      });
      sums[0] = "合计";
      sums[6] =
        "系部数:" +
        linq
          .from(list)
          .distinct("$.BMMC")
          .count();
      sums[8] =
        "教师数:" +
        linq
          .from(list)
          .distinct("$.GH")
          .count();
      sums[11] =
        "专业数:" +
        linq
          .from(list)
          .distinct("$.ZYDM")
          .count();
      sums[13] =
        "方向数:" +
        linq
          .from(list)
          .distinct("$.ZYFXDM")
          .count();
      sums[15] =
        "课程数:" +
        linq
          .from(list)
          .distinct("$.KCDM")
          .count();
      sums[16] =
        "A类:" +
        linq
          .from(list)
          .where('$.XZ_KCLX == "A类"')
          .count();
      sums[16] +=
        ",B类:" +
        linq
          .from(list)
          .where('$.XZ_KCLX == "B类"')
          .count();
      sums[16] +=
        ",C类:" +
        linq
          .from(list)
          .where('$.XZ_KCLX == "C类"')
          .count();
      sums[18] =
        "主讲" +
        linq
          .from(list)
          .where('$.XZ_SKRW == "主讲"')
          .count();
      sums[19] =
        "合班" +
        linq
          .from(list)
          .where('$.XZ_SFHBSK == "是"')
          .count();
      sums[20] =
        "平行班" +
        linq
          .from(list)
          .where('$.XZ_SFPXB == "是"')
          .count();
      sums[21] =
        "行政班" +
        linq
          .from(list)
          .where('$.XZ_SFXZB == "是"')
          .count();
      sums[23] = "总学时" + linq.from(list).sum("$.JXGZL");
      sums[27] =
        "通过" +
        linq
          .from(list)
          .where('$.审核状态 =="通过"')
          .count();
      sums[27] +=
        ",不通过" +
        linq
          .from(list)
          .where('$.审核状态 =="不通过"')
          .count();
      sums[27] +=
        ",待审核" +
        linq
          .from(list)
          .where('$.审核状态 =="待审核"')
          .count();
      //   sums[5]= "领导数:"+linq.from(list).distinct("$.GH").count();
      //  sums[7]= "专职:"+linq.from(list).sum("$.JZGS_ZZ");
      //   sums[8]= "兼职:"+linq.from(list).sum("$.JZGS_JZ");
      //   sums[9]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
      //     sums[9] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
      return sums;
      //
    },
    check(array, list) {
      return new Promise(async (resolve, reject) => {
        try {
          var user = getUser();
        
          var 课程 = await get课程设置(user.XXID, user.CJPC);
   
          var err = linq
            .from(array)
            .where("!$.error")
            .select("$.GH")
            .except(
              linq
                .from(this.$root.教师基本情况)
                .select("$.GH")
                .toArray()
            )
            .toArray();
          
          linq
            .from(array)
            .where(e => err.indexOf(e.GH) !== -1)
            .forEach(
              e =>
              
                (e.error = e.error
                  ? e.error + ";"
                  : "" + "教师基本情况表不存在对应的教工号")
            );
          var data = linq
            .from(array)
            .where("!$.error")
            .select("$.KCDM+','+$.ZYDM + ',' + $.ZYFXDM")
            .except(
              linq
                .from(课程)
                .select("$.KCDM+','+$.ZYDM + ',' + $.ZYFXDM")
                .toArray()
            )
            .toArray();

          linq
            .from(array).where("$.ZYDM !=='999999'")
            .where(
              e => data.indexOf(e.KCDM + "," + e.ZYDM + "," + e.ZYFXDM) !== -1
            )
            .forEach(e => {
              e.error = e.error
                ? e.error + ";"
                : "" + "课程设置表不存在专业代码、专业方向代码与课程代码组合";
            });
          
          linq
            .from(array)
        
            .forEach(e => {
              
              var d = linq.from(this.$root.教师基本情况).firstOrDefault(item => item.GH == e.GH);
              
              if (d) {
                e.教师性质 = d.教师性质;
                e.XM = d.XM;
              }
            });
            linq
            .from(array)
            .forEach(e=>{
              var  kc = linq.from(课程).firstOrDefault(`$.ZYDM=='${e.ZYDM}' && $.ZYFXDM=='${e.ZYFXDM}' && $.KCDM == '${e.KCDM}'`,null);
              if(kc){
                e.KCMC = kc.KCMC;
                e.ZYMC = kc.ZYMC;
                e.ZYFXMC = kc.ZYFXMC;
              }
            })
          resolve();
        } catch (e) {
          reject(e);
        }
      });
    },

    onDlgCreated(model) {
      // console.log(this.$root.开设专业)
      // var dd = findComponent(this, 'BMMC')
      // var 专业 = linq
      //   .from(this.$root.开设专业)
      //   .distinct('$.BMMC')
      //   .toArray()
      // dd.$attrs.items = 专业.map(item => {
      //   return { label: item.BMMC, value: item.BMMC }
      // })
      // console.log(this.$root.开设专业)
    },

    async onChange(model, field, value) {

      console.log(model, field, value);
      // if (field == "KCMC") {
      //   if (value == "error") {
      //       model.XZ_SKNJ = ""
      //       model.ZYDM = ""
      //       model.ZYMC = ""
      //       model.ZYFXDM = ""
      //       model.ZYFXMC = ""
      //       model.KCDM = ""
      //       model.KCMC = ""
      //       model.XZ_KCLX = ""
      //       model.XZ_KCSX = ""
      //   } else {
         
      //     var c = await get课程设置ByID(
      //       this.$root.user.XXID,
      //       this.$root.user.CJPC,
      //       value
      //     );
          
      //       model.XZ_SKNJ = c.XZ_SKNJ
      //       model.ZYDM = c.ZYDM
      //       model.ZYMC = c.ZYMC
      //       model.ZYFXDM = c.ZYFXDM
      //       model.ZYFXMC = c.ZYFXMC
      //       model.KCDM = c.KCDM
      //       model.XZ_KCLX = c.XZ_KCLX
      //       model.XZ_KCSX = c.XZ_KCSX
      //       model.KCMC = c.kCMC
            
      
      //   }
        if (field == "GH") {
          if (value == "error") {
            console.log("error");

            model.GH = "";
            model.BMBH = "";
            model.BMMC = "";
            model.ZYDM = "";
            model.ZYMC = "";
            model.教师性质 = "";
            model.ZYFXDM = "";
          } else {
            var d1 = await get教师基本情况ByGH(
              this.$root.user.XXID,
              this.$root.user.CJPC,
              value
            );
            console.log(d1);
            model.GH = d1[0].GH;
            model.XM = d1[0].XM;
            model.教师性质 = d1[0].教师性质;
            model.BMBH = d1[0].BMBH;
            // model.ZYFXDM=d1[0].ZYFXDM
            // model.ZYFXMC = d1[0].ZYFXMC
            model.BMMC = d1[0].BMMC;
          }
        }
      //}
    },
    async fetchData() {
      var res = [];
      var user = getUser();
      var 课程 = await get课程设置(user.XXID, user.CJPC);

      var datas = await get教师授课情况(user.XXID, user.CJPC);
      var 教工 = await get教师基本情况(user.XXID, user.CJPC);

      var param = this.$route.query.param;

      if (param) {
        datas = linq
          .from(datas)
          .join(
            教工,
            e => e.GH,
            e => e.GH,
            (outer, inner) => {
              outer.教师性质 = inner.教师性质;
              return outer;
            }
          )
          .where(`$.教师性质 === '${param.教师性质}'`)
          .toArray();
      }

      //  var resl = linq.from(res).where(x=>x.GH == "b535d1ef513cad3a7883cf5de509e1a2326ce71c4dd8ceee448578b748901666").toArray()

      console.log(res, datas);

      return datas;
    },
    add_check(model, list) {
      //
      // 是否已经存在数据
      console.log(list, model);
      //
    },
    onadd(model) {
      //

      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        console.log(JSON.stringify(temp));
        var model = await add教师授课情况(temp);
        this.add_check();
        resolve(model);
      });
    },
    async bulkAdd(array) {
      await bulkAddJsskqk({ Jsskqk: array });
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        console.log(JSON.stringify(temp));
        var model = await update教师授课情况(temp);
        resolve(model);
      });
      //
    },
    ondelete(model) {
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        await delete教师授课情况(temp);
        resolve();
      });
    }
  }
};
</script>
<style>
</style>
